//
//  CSTestOptionViewController.m
//  CarService
//
//  Created by fengyn on 15/5/27.
//  Copyright (c) 2015年 lexingsoft. All rights reserved.
//

#import "CSTestOptionViewController.h"
#import "NetworkConfig.h"
#import "Services.h"
#import <UIAlertView-Blocks/UIAlertView+Blocks.h>

@interface CSTestOptionViewController ()
@property (weak, nonatomic) IBOutlet UITableView *ui_tableView;
@property (weak, nonatomic) IBOutlet UILabel *ui_currentLabel;
@property (strong, nonatomic) NSMutableArray *hostArray;

@end

@implementation CSTestOptionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title=@"测试选项";
    UIBarButtonItem *addItem=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addAction:)];
    self.navigationItem.rightBarButtonItem=addItem;
    NSString *currentHost=[ServiceManager getHostAddress];
    self.hostArray=[NSMutableArray arrayWithArray:@[currentHost,@"http://192.168.2.62",@"http://192.168.2.51",@"http://192.168.2.219",@"http://192.168.2.222:8080",@"http://192.168.2.143:8080"]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self updateCurrentData];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

-(void)updateCurrentData{
    NSString *currentHost=[ServiceManager getHostAddress];
    self.ui_currentLabel.text=[NSString stringWithFormat:@"当前Host:  %@",currentHost];
}

-(void)addAction:(id)sender
{
    UIAlertView *alertView=[[UIAlertView alloc] init];
    alertView.alertViewStyle=UIAlertViewStylePlainTextInput;
    alertView.title=@"添加Host";
    UITextField *textField=[alertView textFieldAtIndex:0];
    textField.text=@"http://";
    
    RIButtonItem *cancelItem=[RIButtonItem itemWithLabel:@"取消"];
    RIButtonItem *okItem=[RIButtonItem itemWithLabel:@"确定" action:^{
        NSString *address=textField.text;
        [self.hostArray addObject:address];
        [self.ui_tableView reloadData];
    }];
    [alertView addButtonItem:cancelItem];
    [alertView addButtonItem:okItem];
    [alertView show];
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger row=0;
    row=self.hostArray.count;
    return row;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellIdentifier=@"StaticCellIdentifier";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.textLabel.text=self.hostArray[indexPath.row];
    return cell;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [DataManager sharedInstance].testHost=self.hostArray[indexPath.row];
    [self updateCurrentData];
}
@end
